
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.mixins import ListModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import GenericViewSet
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

from order.filter import OrderFilter
from order.models import Order
from order.serializers import OrderSerializer


class OrderPagination(PageNumberPagination):
    page_size = 1
    page_query_param = 'page'


class OrderListViewSet(ListModelMixin, GenericViewSet):
    serializer_class = OrderSerializer
    authentication_classes = [BasicAuthentication, SessionAuthentication, JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]
    # queryset = Order.objects.all()

    # 过滤器字段
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    # filterset_fields = ('total', 'number')
    ordering_fields = ('total',)

    filter_class = OrderFilter

    pagination_class = OrderPagination

    def get_queryset(self):
        orders = Order.objects.filter(user=self.request.user)
        return orders